﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.TrainUI {
    import flash.events.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Modules.workers.*;
    import Display.*;
    import flash.text.*;
    import Classes.inventory.*;
    import Display.aswingui.*;

    public class TrainStationDialogView extends GenericDialogView {

        protected var m_items:Array;
        private var m_txtTime:JLabel;
        private var m_txtReward:JLabel;
        private var m_scrollingListPanel:JPanel;
        private var m_button:CustomButton;
        private var m_trainManager:TrainManager;
        private var m_btnSpeedUp:CustomButton;
        private var m_btnPurchaseStation:CustomButton;
        private var m_speedUpPanel:JPanel;
        private var m_purchaseStationPanel:JPanel;
        private var m_grantedBuyPermissionForStationPurchase:Boolean = false;
        private var m_hasOverlay:Boolean = false;

        public function TrainStationDialogView(_arg1:Dictionary, _arg2:String="", _arg3:String="", _arg4:int=0, _arg5:Function=null, _arg6:String="", _arg7:int=0, _arg8:String="", _arg9:Function=null, _arg10:String=""){
            this.m_trainManager = Global.world.citySim.trainManager;
            super(_arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10);
            this.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
        }
        public function get hasOverlay():Boolean{
            return (this.m_hasOverlay);
        }
        override protected function init():void{
            m_bgAsset = m_assetDict["dialog_bg"];
            this.makeBackground();
            makeCenterPanel();
            ASwingHelper.prepare(this);
            this.m_trainManager.addEventListener(TrainManagerEvent.TIME_UPDATE, this.timeUpdateHandler);
            this.m_trainManager.addEventListener(TrainManagerEvent.REWARD_UPDATE, this.rewardUpdateHandler);
            this.m_trainManager.addEventListener(TrainManagerEvent.TRAIN_STATE_UPDATE, this.trainStateUpdateHandler);
            this.m_trainManager.addEventListener(TrainManagerEvent.TRAIN_STATION_LIST_UPDATE, this.trainStationListUpdateHandler);
        }
        override protected function makeBackground():void{
            super.makeBackground();
            setPreferredWidth(m_bgAsset.width);
        }
        override protected function createInteriorHolder():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local1.append(ASwingHelper.makeMultilineText(ZLoc.t("Dialogs", "TrainUI_TrainStation_message"), 500, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.CENTER, 16, EmbeddedArt.brownTextColor));
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel();
            _local2.appendAll(this.createStationList(), this.createLogisticsPanel());
            ASwingHelper.setEasyBorder(_local2, 0, 10, 0, 10);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            _local3.appendAll(createHeaderPanel(), ASwingHelper.verticalStrut(20), _local1, ASwingHelper.verticalStrut(20), _local2, ASwingHelper.verticalStrut(60), this.createButtonPanel());
            return (_local3);
        }
        override protected function createButtonPanel():JPanel{
            var _local1:JPanel = new JPanel(new FlowLayout(AsWingConstants.CENTER));
            this.m_button = new CustomButton(this.getGreenButtonStringBasedOnTrainState(), null, "GreenButtonUI");
            this.m_button.addActionListener(this.btnRequestStationClickHandler, 0, true);
            _local1.append(this.m_button);
            return (_local1);
        }
        protected function createStationList():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local1.append(ASwingHelper.makeTextField(ZLoc.t("Dialogs", "TrainUI_CurrentStations"), EmbeddedArt.TITLE_FONT, 20, EmbeddedArt.blueTextColor));
            this.m_scrollingListPanel = ASwingHelper.makeSoftBoxJPanelVertical();
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical();
            _local2.setPreferredWidth(460);
            _local2.appendAll(_local1, this.m_scrollingListPanel);
            this.updateScrollingList();
            return (_local2);
        }
        private function updateScrollingList():void{
            this.m_scrollingListPanel.removeAll();
            var _local1:Array = (this.m_trainManager.isWaitingForTrain) ? this.m_trainManager.trainStopData : this.m_trainManager.candidateTrainStopData;
            var _local2:TrainStationScrollingList = new TrainStationScrollingList(_local1, 0);
            ASwingHelper.prepare(_local2);
            this.m_scrollingListPanel.append(_local2);
            this.m_scrollingListPanel.setPreferredHeight(TrainStopCell.CELL_HEIGHT);
            ASwingHelper.prepare(this);
        }
        protected function createLogisticsPanel():JPanel{
            var _local2:int;
            var _local3:String;
            var _local4:int;
            var _local5:int;
            var _local6:int;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical();
            if (!this.m_trainManager.isWaitingForTrain){
                _local2 = this.m_trainManager.purchaseCandidateItem.trainTripTime;
                _local3 = ((this.m_trainManager.purchaseCandidateItem.goods > 0)) ? TrainWorkers.OP_SELL : TrainWorkers.OP_BUY;
                _local4 = Global.gameSettings.getTieredInt(this.m_trainManager.purchaseCandidateItem.trainPayout, 0);
                _local5 = this.m_trainManager.purchaseCandidateItem.trainSpeedUpCost;
                _local6 = this.m_trainManager.purchaseCandidateItem.workers.cashCost;
            } else {
                _local2 = this.m_trainManager.trainArrivalTime;
                _local3 = this.m_trainManager.currentTrip.transactionType;
                _local4 = this.m_trainManager.trainRewardAmount;
                _local5 = this.m_trainManager.currentTrip.item.trainSpeedUpCost;
                _local6 = this.m_trainManager.currentTrip.item.workers.cashCost;
            };
            this.m_txtTime = ASwingHelper.makeLabel(GameUtil.formatMinutesSeconds(_local2), EmbeddedArt.TITLE_FONT, 24, EmbeddedArt.darkBlueTextColor);
            var _local7:JPanel = ASwingHelper.makeSoftBoxJPanel();
            _local7.appendAll(new AssetPane(m_assetDict["clock"]), this.m_txtTime);
            this.m_speedUpPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_txtReward = ASwingHelper.makeLabel(_local4.toString(), EmbeddedArt.TITLE_FONT, 24, EmbeddedArt.greenTextColor);
            var _local8:JPanel = ASwingHelper.makeSoftBoxJPanel();
            _local8.appendAll(new AssetPane(((_local3 == TrainWorkers.OP_BUY)) ? m_assetDict["crate_big"] : m_assetDict["coin_big"]), this.m_txtReward);
            this.m_btnSpeedUp = new CustomButton(ZLoc.t("Dialogs", "TrainUI_speedUp", {amount:_local5}), new AssetIcon(new EmbeddedArt.icon_cash()), "CashButtonUI");
            this.m_btnSpeedUp.addActionListener(this.btnSpeedUpClickHandler);
            this.m_speedUpPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            this.m_speedUpPanel.append(this.m_btnSpeedUp);
            this.m_btnPurchaseStation = new CustomButton(ZLoc.t("Dialogs", "TrainUI_purchaseStation", {amount:_local6}), new AssetIcon(new EmbeddedArt.icon_cash()), "CashButtonUI");
            this.m_btnPurchaseStation.addActionListener(this.btnPurchaseStationClickHandler);
            this.m_purchaseStationPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            this.m_purchaseStationPanel.append(this.m_btnPurchaseStation);
            _local1.appendAll(_local7, ASwingHelper.verticalStrut(10), this.m_speedUpPanel, ASwingHelper.verticalStrut(10), _local8, ASwingHelper.verticalStrut(10), this.m_purchaseStationPanel);
            this.updateButtonStatus();
            return (_local1);
        }
        protected function getGreenButtonStringBasedOnTrainState():String{
            var _local1 = "";
            if (this.m_trainManager.isWaitingForTrain){
                if (this.m_trainManager.isTrainAwaitingClearance){
                    _local1 = ZLoc.t("Dialogs", "TrainUI_ReceiveWaitingTrain");
                } else {
                    _local1 = ZLoc.t("Dialogs", "TrainUI_RequestStations");
                };
            } else {
                _local1 = ZLoc.t("Dialogs", "TrainUI_DispatchTrainAndRequestStations");
            };
            return (_local1);
        }
        private function updateButtonStatus():void{
            this.m_btnSpeedUp.setEnabled(this.m_trainManager.canSpeedUp);
            this.m_btnPurchaseStation.setEnabled(this.m_trainManager.canBuyNewStations);
        }
        override protected function closeMe():void{
            this.m_trainManager.removeEventListener(TrainManagerEvent.TIME_UPDATE, this.timeUpdateHandler);
            this.m_trainManager.removeEventListener(TrainManagerEvent.REWARD_UPDATE, this.rewardUpdateHandler);
            this.m_trainManager.removeEventListener(TrainManagerEvent.TRAIN_STATE_UPDATE, this.trainStateUpdateHandler);
            this.m_trainManager.removeEventListener(TrainManagerEvent.TRAIN_STATION_LIST_UPDATE, this.trainStationListUpdateHandler);
            super.closeMe();
        }
        private function btnRequestStationClickHandler(_arg1:Event):void{
            var _local2:int;
            var _local3:int;
            var _local4:String;
            if (this.m_trainManager.isWaitingForTrain){
                if (this.m_trainManager.isTrainAwaitingClearance){
                    if (this.m_trainManager.currentTrip.transactionType == TrainWorkers.OP_BUY){
                        _local2 = Global.player.commodities.getSpareCapacity(Commodities.GOODS_COMMODITY);
                        _local3 = this.m_trainManager.currentTrip.commodityAmount;
                        if (_local2 < _local3){
                            _local4 = ZLoc.t("Dialogs", "TrainUI_confirmAcceptOverflowTrain", {amount:(_local3 - _local2)});
                            UI.displayMessage(_local4, GenericDialogView.TYPE_YESNO, this.grantClearanceConfirmClickHandler, "", true);
                            this.m_hasOverlay = true;
                        } else {
                            this.grantClearanceConfirmClickHandler(null);
                        };
                    } else {
                        this.grantClearanceConfirmClickHandler(null);
                    };
                } else {
                    this.m_trainManager.popRequestStationsFeed();
                };
            } else {
                this.m_trainManager.performSchedulePurchase();
                this.m_trainManager.popRequestStationsFeed();
            };
            this.closeMe();
        }
        private function grantClearanceConfirmClickHandler(_arg1:GenericPopupEvent):void{
            if (((!(_arg1)) || ((_arg1.button == GenericDialogView.YES)))){
                this.m_trainManager.scrollToTrainStation();
                this.m_trainManager.grantTrainClearance();
            };
        }
        private function btnSpeedUpClickHandler(_arg1:Event):void{
            var _local2:String = ZLoc.t("Dialogs", "TrainUI_confirmPurchaseSpeedUp", {amount:this.m_trainManager.currentTrip.item.trainSpeedUpCost});
            UI.displayMessage(_local2, GenericDialogView.TYPE_YESNO, this.speedUpConfirmClickHandler, "", true);
            this.m_hasOverlay = true;
        }
        private function speedUpConfirmClickHandler(_arg1:GenericPopupEvent):void{
            this.m_hasOverlay = false;
            if (_arg1.button == GenericDialogView.YES){
                this.m_trainManager.speedUpCurrentTrain();
            };
        }
        private function btnPurchaseStationClickHandler(_arg1:Event):void{
            var _local2:String;
            this.m_hasOverlay = false;
            if (this.m_grantedBuyPermissionForStationPurchase){
                this.m_trainManager.purchaseNewStation();
            } else {
                _local2 = ZLoc.t("Dialogs", "TrainUI_confirmPurchaseStation", {amount:this.m_trainManager.currentTrip.item.workers.cashCost});
                UI.displayMessage(_local2, GenericDialogView.TYPE_YESNO, this.purchaseStationConfirmClickHandler, "", true);
                this.m_hasOverlay = true;
            };
        }
        private function purchaseStationConfirmClickHandler(_arg1:GenericPopupEvent):void{
            this.m_hasOverlay = false;
            if (_arg1.button == GenericDialogView.YES){
                this.m_trainManager.purchaseNewStation();
                this.m_grantedBuyPermissionForStationPurchase = true;
            };
        }
        private function timeUpdateHandler(_arg1:TrainManagerEvent):void{
            this.m_txtTime.setText(GameUtil.formatMinutesSeconds(this.m_trainManager.trainArrivalTime));
            this.updateButtonStatus();
            ASwingHelper.prepare(this);
        }
        private function rewardUpdateHandler(_arg1:TrainManagerEvent):void{
            this.m_txtReward.setText(this.m_trainManager.trainRewardAmount.toString());
            this.updateButtonStatus();
        }
        private function trainStateUpdateHandler(_arg1:TrainManagerEvent):void{
            this.m_button.setText(this.getGreenButtonStringBasedOnTrainState());
            this.updateButtonStatus();
            ASwingHelper.prepare(this);
        }
        private function trainStationListUpdateHandler(_arg1:TrainManagerEvent):void{
            this.m_txtReward.setText(this.m_trainManager.trainRewardAmount.toString());
            this.updateScrollingList();
        }

    }
}//package Display.TrainUI 
